package com.nuance.assignment.fileInfo.client.view;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsonUtils;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestOracle;
import com.nuance.assignment.fileInfo.client.util.FileInfoConstants;
import com.nuance.assignment.fileInfo.shared.StringArrayResponseJavaScriptObject;

/**
 * Suggest Oracle for retrieving directory name suggestions
 * Does not use GWT-RPC, uses REST instead.
 * 
 * @author parag
 *
 */
public class FileInfoSuggestOracle extends MultiWordSuggestOracle {

	private final FileInfoConstants fileInfoConstants;
	
	private static class SimpleSuggestion implements Suggestion{
		
		private final String text;
		
		public SimpleSuggestion(String text) {
			this.text = text;
		}
		
		@Override
		public String getDisplayString() {
			return text;
		}
		
		@Override
		public String getReplacementString() {
			return text;
		}
	}
	
	public FileInfoSuggestOracle(FileInfoConstants fileInfoConstants) {
		this.fileInfoConstants = fileInfoConstants;
	}
	
	private String createUrl(String queryString, int size){
		return GWT.getHostPageBaseURL() + fileInfoConstants.suggestionsUrl() + URL.encodeQueryString(queryString)
				+"&size="+size;
	}
	
	@Override
	public void requestSuggestions(final Request suggestOracleRequest, final Callback callback) {

		RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, createUrl(suggestOracleRequest.getQuery(), suggestOracleRequest.getLimit()));
		try {
			requestBuilder.sendRequest(null, new com.google.gwt.http.client.RequestCallback(){
				@Override
				public void onResponseReceived(
						com.google.gwt.http.client.Request request,
						com.google.gwt.http.client.Response response) {
					StringArrayResponseJavaScriptObject responseJSObject = JsonUtils.safeEval(response.getText());
					if(responseJSObject.isSuccess()){
						SuggestOracle.Response suggestOracleResponse = new SuggestOracle.Response();
						List<SimpleSuggestion> simpleSuggestions = new ArrayList<>();
						for(String suggestion : responseJSObject.getPalyload()){
							simpleSuggestions.add(new SimpleSuggestion(suggestion));
						}
						suggestOracleResponse.setSuggestions(simpleSuggestions);
						callback.onSuggestionsReady(suggestOracleRequest, suggestOracleResponse);
					}else{
						String combinedMessage = "";
						for(String message : responseJSObject.getMessages()){
							combinedMessage += message+"\n";
						}
						Window.alert(combinedMessage);
					}
				}

				@Override
				public void onError(com.google.gwt.http.client.Request request,
						Throwable exception) {
					Window.alert(exception.getMessage());
				}
			});
		} catch (RequestException exception) {
			Window.alert(exception.getMessage());
		}
	}
}
